﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObject;
using DataBase;

namespace BusinessLayer
{
    public class UsersLogic
    {
        public static int count = 0;
        public static string logIn = "";
        UserDbFunctionality DbObj = new UserDbFunctionality();
        public void AddData(UserDao user)
        {
            DbObj.AddUser(user);
        }
        public void UpdateUser(UserDao User , string PaNumber)
        {
            DbObj.Update(User,PaNumber);
        }
        public List<UserDao> PendingRequests()
        {
            List<UserDao> List = new List<UserDao>();
            List = DbObj.PendingUsers();
            return List;
        }
        public void ChangeStatus(string PaNum)
        {
            DbObj.ChangeStatus(PaNum);
        }
        public bool PACheck(string PaNum)
        {
            bool result = DbObj.CheckPaNumberAvalibility(PaNum);
            return result;
        }
        public string Login(UserDao InUser)
        {
            string result = "";
            UserDao CompUser = new UserDao();
            CompUser = DbObj.AdminLoginDb();
            CompUser.PaNumber = "";
            if (InUser.PaNumber.Equals("admin123"))
            {
                if (InUser.Email.Equals(CompUser.Email) && InUser.Password.Equals(CompUser.Password))
                {
                    result = "1";
                }
                else
                {
                    result = "Wrong Email Id Or Password";
                }
            }
            else
            {
                string PaNum = InUser.PaNumber;
                CompUser = DbObj.UserLogin(PaNum);
                if (CompUser.Rank.Equals("Pending"))
                {
                    count = 0;
                    result = "Your Account Has Not Yet Been Activated";
                }
                else if (CompUser.Rank.Equals("Disabled"))
                {
                    count = 0;
                    result = "Sorry Your Account Is Disabled Please Contact Admin";
                }
                else if (CompUser.Rank.Equals("5"))
                {
                    count = 0;
                    result = "Sorry No Account Exists With That PA Number";
                }
                else
                {
                    if (InUser.Email != CompUser.Email || InUser.Password != CompUser.Password)
                    {
                        if (CompUser.PaNumber != logIn)
                        {
                            count = 1;
                            logIn = CompUser.PaNumber;
                            result = "Sorry Wrong Email Or Password";
                        }
                        else
                        {
                            count++;
                            if (count == 3)
                            {
                                result = "Your Account Has Been Disabled Because Of Sucessive Wrong Information";
                                
                                DbObj.DisableAccount(logIn);
                                logIn = "";
                                count = 0;
                            }
                            else
                            {
                                result = "Sorry Wrong Email Or Password";
                            }
                        }

                    }
                    else
                    {
                        result = "8";
                    }
                }
            }
            return result;
        }
    }
    public class ReservationLogic
    {
        ReservationDbFunctionality DbObj = new ReservationDbFunctionality();
        UserDbFunctionality UserObj = new UserDbFunctionality();
        public List<ReservationDao> PendingRequests()
        {
            List<ReservationDao> List = new List<ReservationDao>();
            List = DbObj.PendingUsers();
            return List;
        }
        public List<ReservationDao> MyReservation(string Start, string End, string PaNumber)
        {
            List<ReservationDao> List = new List<ReservationDao>();
            try
            {
                DateTime StartOk = Convert.ToDateTime(Start);
                DateTime EndOk = Convert.ToDateTime(End);
                List = DbObj.MyReservation(StartOk, EndOk, PaNumber);
                return List;
            }
            catch (FormatException)
            {
                ReservationDao Obj = new ReservationDao();
                List.Add(Obj);
                List[0].PaNumber = "Wrong";
                return List;
            }
        }
        public void ConformReservation(string Room, string Request)
        {
            ReservationDao Reservation = new ReservationDao();
            Reservation = DbObj.ExtractInfoFromReservation(Request);
            Reservation.Status = "Reserved";
            Reservation.RoomNo = Room;
            DbObj.ConformReservation(Reservation);
            DbObj.UpdateStatus(Request, Room);
        }
        public bool CheckRooms()
        {
            bool flag = false;
            flag = DbObj.CheckRooms();
            return flag;
        }
        public int CheckReservedRooms()
        {
            int result = 0;
            result = DbObj.ReservedRooms();
            return result;
        }
        public ReservationDao Obj_Cancell_Reservation(string RoomNo)
        {
            ReservationDao Reservation = new ReservationDao();
            Reservation = DbObj.Obj_Cancell_Reservation(RoomNo);
            return Reservation;
        }
        public void ConfirmCancelation(string RoomNo)
        {
            DbObj.ConfirmCancelation(RoomNo);
        }
        public bool ReserveRoom(ReservationDao Reservation)
        {
            try
            {
                DateTime In = Convert.ToDateTime(Reservation.CheckIn);
                DateTime Out = Convert.ToDateTime(Reservation.CheckOut);
                string date = DateTime.Now.ToString("d");
                DateTime ToDay = Convert.ToDateTime(date);
                //Reservation.CheckOut = ToDay.ToString();
                System.TimeSpan span = Out - In;
                int days = (int)span.TotalDays;
                System.TimeSpan span1 = In - ToDay;
                int days1 = (int)span1.TotalDays;
                Reservation.Status = "Pending";
                if (Out <= In)
                {
                    return false;
                }
                else if (days > 3)
                {
                    return false;
                }
                else if (days1 < 0 || days1 > 5)
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }
            UserDao user = new UserDao();
            user = UserObj.GetUser(Reservation.PaNumber);
            Reservation.Rank = user.Rank;
            Reservation.Name = user.Name;
            DbObj.SaveReservation(Reservation);
            return true;
        }
    }
}
